﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace FlexExplorer
{
	public partial class NewExplorerForm : Form
	{
		private ExplSlave slave;
		private string currentLocalPath = String.Empty;
		private string currentRemotePath = String.Empty;

		const string DIRECTORY_IDENTIFIER = "<DIR>";
		const string GO_UPPER_IDENTIFIER = "<UP>";

		public NewExplorerForm(ExplSlave slave)
		{
			InitializeComponent();
			this.slave = slave;
			
			DriveInfo[] drives = DriveInfo.GetDrives();
			foreach (DriveInfo drive in drives)
			{
				try
				{
					// TODO: DriveType icons
					if (drive.IsReady)
					{
						string driveLetter = drive.Name[0].ToString() + drive.Name[1].ToString();						
						LHDriveSelector.Items.Add(driveLetter);
					}
				}
				catch {}
			}
			LHDriveSelector.SelectedItem = LHDriveSelector.Items[0];

			DriveInfo[] remote_drives = slave.GetDrives();
			foreach (DriveInfo rdrive in remote_drives)
			{
				try
				{
					if (rdrive.IsReady)
					{
						string driveLetter = rdrive.Name[0].ToString() + rdrive.Name[1].ToString();
						RHDriveSelector.Items.Add(driveLetter);
					}
				}
				catch {}
			}
			RHDriveSelector.SelectedItem = RHDriveSelector.Items[0];
		}

		private void ShowLocalFiles(string path)
		{
			int i = 0;
			LHFilesList.Items.Clear();

			if (Directory.GetParent(path) != null)
			{
				LHFilesList.Items.Add("..");
				LHFilesList.Items[i].SubItems.Add(GO_UPPER_IDENTIFIER);
				i++;
			}

			string[] subdirs;
			try
			{
				subdirs = Directory.GetDirectories(path);
			}
			catch
			{
				return;
			}
			
			foreach (string subdir in subdirs)
			{
				LHFilesList.Items.Add(Path.GetFileName(subdir));
				LHFilesList.Items[i].SubItems.Add(DIRECTORY_IDENTIFIER);
				LHFilesList.Items[i].SubItems.Add(String.Empty);
				try
				{
					DateTime LastWriteTime = Directory.GetLastWriteTime(path + subdir);
					LHFilesList.Items[i].SubItems.Add(LastWriteTime.ToString());
				}
				catch
				{
				}
				i++;
			}

			string[] files;
			try
			{
				files = Directory.GetFiles(path);
			}
			catch { return; }

			foreach (string file in files)
			{
				LHFilesList.Items.Add(Path.GetFileNameWithoutExtension(file));
				LHFilesList.Items[i].SubItems.Add(Path.GetExtension(file));
				FileInfo fi = new FileInfo(file);
				double fileSize = fi.Length;
				char id = '\0';

				if (fileSize >= 1024)
				{
					fileSize /= 1024;
					id = 'K';
					if (fileSize >= 1024)
					{
						fileSize /= 1024;
						id = 'M';
						if (fileSize >= 1024)
						{
							fileSize /= 1024;
							id = 'G';
							if (fileSize >= 1024)
							{
								fileSize /= 1024;
								id = 'T';
							}
						}
					}
				}

				fileSize = Math.Round(fileSize, 2);

				if (id != '\0')
					LHFilesList.Items[i].SubItems.Add(fileSize + " " + id + "b");
				else
					LHFilesList.Items[i].SubItems.Add(fileSize + " Bytes");
				try
				{
					DateTime LastWriteTime = File.GetLastWriteTime(file);
					LHFilesList.Items[i].SubItems.Add(LastWriteTime.ToString());
				}
				catch { }
				i++;
			}
		}

		private void ShowRemoteFiles(string path)
		{
			int i = 0;
			RHFilesList.Items.Clear();

			if (slave.GetParent(path) != null)
			{
				RHFilesList.Items.Add("..");
				RHFilesList.Items[i].SubItems.Add(GO_UPPER_IDENTIFIER);
				i++;
			}

			string[] subdirs;
			try
			{
				subdirs = slave.GetDirectories(path);
			}
			catch
			{
				return;
			}

			foreach (string subdir in subdirs)
			{
				RHFilesList.Items.Add(Path.GetFileName(subdir));
				RHFilesList.Items[i].SubItems.Add(DIRECTORY_IDENTIFIER);
				RHFilesList.Items[i].SubItems.Add(String.Empty);
				try
				{
					DateTime LastWriteTime = slave.GetDirectoryLastWriteTime(path + subdir);
					RHFilesList.Items[i].SubItems.Add(LastWriteTime.ToString());
				}
				catch
				{
				}
				i++;
			}

			string[] files;
			try
			{
				files = slave.GetFiles(path);
			}
			catch { return; }

			foreach (string file in files)
			{
				RHFilesList.Items.Add(Path.GetFileNameWithoutExtension(file));
				RHFilesList.Items[i].SubItems.Add(Path.GetExtension(file));
				FileInfo fi = slave.GetFileInfo(file);
				double fileSize = fi.Length;
				char id = '\0';

				if (fileSize >= 1024)
				{
					fileSize /= 1024;
					id = 'K';
					if (fileSize >= 1024)
					{
						fileSize /= 1024;
						id = 'M';
						if (fileSize >= 1024)
						{
							fileSize /= 1024;
							id = 'G';
							if (fileSize >= 1024)
							{
								fileSize /= 1024;
								id = 'T';
							}
						}
					}
				}

				fileSize = Math.Round(fileSize, 2);

				if (id != '\0')
					RHFilesList.Items[i].SubItems.Add(fileSize + " " + id + "b");
				else
					RHFilesList.Items[i].SubItems.Add(fileSize + " Bytes");

				try
				{
					DateTime LastWriteTime = slave.GetFileLastWriteTime(file);
					RHFilesList.Items[i].SubItems.Add(LastWriteTime.ToString());
				}
				catch {	}
				i++;
			}
		}

		private void LHDriveSelector_SelectedIndexChanged(object sender, EventArgs e)
		{
			LHPathBox.Text = currentLocalPath = LHDriveSelector.SelectedItem.ToString() + "\\";
			ShowLocalFiles(currentLocalPath);
		}

		private void RHDriveSelector_SelectedIndexChanged(object sender, EventArgs e)
		{
			RHPathBox.Text = currentRemotePath = RHDriveSelector.SelectedItem.ToString() + "\\";
			ShowRemoteFiles(currentRemotePath);
		}

		private void LHFilesList_ItemActivate(object sender, EventArgs e)
		{
			if (LHFilesList.SelectedItems.Count == 1)
			{
				if (LHFilesList.SelectedItems[0].SubItems[1].Text == DIRECTORY_IDENTIFIER)
					if (slave.GetParent(currentLocalPath) == null)
						LHPathBox.Text = (currentLocalPath += LHFilesList.SelectedItems[0].Text);
					else
						LHPathBox.Text = (currentLocalPath += ("\\" + LHFilesList.SelectedItems[0].Text));
				else if (LHFilesList.SelectedItems[0].SubItems[1].Text == GO_UPPER_IDENTIFIER)
					LHPathBox.Text = currentLocalPath = Directory.GetParent(currentLocalPath).ToString();
				ShowLocalFiles(currentLocalPath);
			}
		}

		private void RHFilesList_ItemActivate(object sender, EventArgs e)
		{
			if (RHFilesList.SelectedItems.Count == 1)
			{
				if (RHFilesList.SelectedItems[0].SubItems[1].Text == DIRECTORY_IDENTIFIER)
					if (slave.GetParent(currentRemotePath) == null)
						RHPathBox.Text = (currentRemotePath += RHFilesList.SelectedItems[0].Text);
					else
						RHPathBox.Text = (currentRemotePath += ("\\" + RHFilesList.SelectedItems[0].Text));
				else if (RHFilesList.SelectedItems[0].SubItems[1].Text == GO_UPPER_IDENTIFIER)
					RHPathBox.Text = currentRemotePath = slave.GetParent(currentRemotePath).ToString();
				ShowRemoteFiles(currentRemotePath);
			}
		}

		// Кароче большой бубен тут. Хочу чтобы при смене лабела итема - переименовывался файл...
		// не могу понять какое событие нужно

		private void LHFilesList_BeforeLabelEdit(object sender, LabelEditEventArgs e)
		{

		}

		private void RHFilesList_BeforeLabelEdit(object sender, LabelEditEventArgs e)
		{
						
		}

		private void LHFilesList_AfterLabelEdit(object sender, LabelEditEventArgs e)
		{
			Rename(LHFilesList.Items[e.Item].Text + LHFilesList.Items[e.Item].SubItems[1].Text, e.Label);
			this.Refresh();
		}
		
		private void RHFilesList_AfterLabelEdit(object sender, LabelEditEventArgs e)
		{
			Rename(RHFilesList.Items[e.Item].Text + RHFilesList.Items[e.Item].SubItems[1].Text, e.Label);
			this.Refresh();
		}

		private const int BLOCK_SIZE = 204800;

		private void RefreshLists()
		{
			if (this.InvokeRequired)
			{
				this.Invoke(new RefreshDelegate(RefreshLists));
				return;
			}

			ShowLocalFiles(currentLocalPath);
			ShowRemoteFiles(currentRemotePath);			
		}

		private void CopyFromLocalToRemote(object _name)
		{
			string name = _name.ToString();
			string from = currentLocalPath + "\\";
			string to = currentRemotePath + "\\";

			copyForm.SetLabel(name);

			BinaryReader reader = new BinaryReader(new FileStream(from + name, FileMode.Open, FileAccess.Read));
			slave.OpenFileToWrite(to + name);

			try
			{
				int fileSize = (int)reader.BaseStream.Length;
				copyForm.SetFileLength(fileSize);
				copyForm.SetFullCopyfileSizeLabel(fileSize.ToString());

				int CopiedBytes = 0;
				int FSize = fileSize;

				while (fileSize > 0)
				{
					int copySize = fileSize > BLOCK_SIZE ? BLOCK_SIZE : fileSize;
					fileSize -= copySize;
					CopiedBytes += copySize;
					double percent = ((double)CopiedBytes / (double)FSize) * 100;

					copyForm.SetCopyPercentsCompleteLabel(((int)percent).ToString());
					copyForm.SetCopiedFileSizeLabel(CopiedBytes.ToString());

					byte[] data = reader.ReadBytes(copySize);
					slave.WriteFile(data);
					copyForm.Advance(copySize);
				}
			}
			catch (ThreadAbortException)
			{
				reader.Close();
				slave.CloseWriter();
				RefreshLists();
			}

			reader.Close();
			slave.CloseWriter();
			copyForm.DoClose();
			RefreshLists();
		}

		private void CopyFromRemoteToLocal(object _name)
		{
			string name = _name.ToString();
			string from = currentRemotePath + '\\'; // Путь к папке на удаленном хосте
			string to = currentLocalPath + '\\';  // Путь к папке на локальном хосте

			copyForm.SetLabel(name);

			BinaryWriter writer = new BinaryWriter(new FileStream(to + name, FileMode.Create, FileAccess.Write));
			int fileSize = (int)slave.OpenFile(from + name);
			copyForm.SetFullCopyfileSizeLabel(fileSize.ToString());

			try
			{
				copyForm.SetFileLength(fileSize);
				int CopiedBytes = 0;
				int FSize = fileSize;

				while (fileSize > 0)
				{
					int copySize = fileSize > BLOCK_SIZE ? BLOCK_SIZE : fileSize;
					fileSize -= copySize;
					CopiedBytes += copySize;
					double percent = ((double)CopiedBytes / (double)FSize) * 100;

					copyForm.SetCopyPercentsCompleteLabel(((int)percent).ToString());
					copyForm.SetCopiedFileSizeLabel(CopiedBytes.ToString());

					byte[] data = slave.ReadFile(copySize);
					writer.Write(data);
					copyForm.Advance(copySize);
				}
			}
			catch (ThreadAbortException)
			{
				writer.Close();
				slave.CloseFile();
				RefreshLists();
				return;
			}

			writer.Close();
			slave.CloseFile();
			copyForm.DoClose();
			RefreshLists();
		}

		private void MoveFromLocalToRemote(object _name)
		{
			string name = _name.ToString();
			string from = currentLocalPath + '\\';
			string to = currentRemotePath + '\\';

			moveForm.SetLabel(name);

			BinaryReader reader = new BinaryReader(new FileStream(from + name, FileMode.Open, FileAccess.Read));
			slave.OpenFileToWrite(to + name);

			try
			{
				int fileSize = (int)reader.BaseStream.Length;
				moveForm.SetFileLength(fileSize);
				moveForm.SetFullMovefileSizeLabel(fileSize.ToString());

				int MovedBytes = 0;
				int FSize = fileSize;

				while (fileSize > 0)
				{
					int moveSize = fileSize > BLOCK_SIZE ? BLOCK_SIZE : fileSize;
					fileSize -= moveSize;
					MovedBytes += moveSize;
					double percent = ((double)MovedBytes / (double)FSize) * 100;

					moveForm.SetMovePercentsCompleteLabel(((int)percent).ToString());
					moveForm.SetMovedFileSizeLabel(MovedBytes.ToString());

					byte[] data = reader.ReadBytes(moveSize);
					slave.WriteFile(data);
					moveForm.Advance(moveSize);
				}
			}
			catch (ThreadAbortException)
			{
				reader.Close();
				slave.CloseWriter();
			}

			reader.Close();
			slave.CloseWriter();
			try
			{
				File.Delete(currentLocalPath + "\\" + name);
			}
			catch (DirectoryNotFoundException)
			{
				MessageBox.Show("Directory not found!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			moveForm.DoClose();
			RefreshLists();
		}

		private void MoveFromRemoteToLocal(object _name)
		{
			string name = _name.ToString();
			string from = currentRemotePath + '\\'; // Путь к папке на удаленном хосте
			string to = currentLocalPath + '\\';  // Путь к папке на локальном хосте

			moveForm.SetLabel(name);

			BinaryWriter writer = new BinaryWriter(new FileStream(to + name, FileMode.Create, FileAccess.Write));
			int fileSize = (int)slave.OpenFile(from + name);

			moveForm.SetFullMovefileSizeLabel(fileSize.ToString());

			try
			{
				moveForm.SetFileLength(fileSize);
				int MovedBytes = 0;
				int FSize = fileSize;

				while (fileSize > 0)
				{
					int moveSize = fileSize > BLOCK_SIZE ? BLOCK_SIZE : fileSize;
					fileSize -= moveSize;
					MovedBytes += moveSize;
					double percent = ((double)MovedBytes / (double)FSize) * 100;

					moveForm.SetMovePercentsCompleteLabel(((int)percent).ToString());
					moveForm.SetMovedFileSizeLabel(MovedBytes.ToString());

					byte[] data = slave.ReadFile(moveSize);
					writer.Write(data);
					moveForm.Advance(moveSize);
				}
			}
			catch (ThreadAbortException)
			{
				writer.Close();
				slave.CloseFile();
				RefreshLists();
			}

			writer.Close();
			slave.CloseFile();
			try
			{
				slave.FileDelete(currentRemotePath + "\\" + name);
			}
			catch (DirectoryNotFoundException)
			{
				MessageBox.Show("Directory not found!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			moveForm.DoClose();
			RefreshLists();
		}

		private CopyProgressForm copyForm;
		private MoveProgressForm moveForm;

		private void Copy()
		{
			if (LHFilesList.Focused) // копируем с локального на удаленный
			{
				int FilesAmount = LHFilesList.SelectedItems.Count;
				if (FilesAmount == 0)
					return;

				for (int i = 0; i < FilesAmount; i++)
				{
					string filename = LHFilesList.SelectedItems[i].Text + LHFilesList.SelectedItems[i].SubItems[1].Text;

					Thread copyThread = new Thread(new ParameterizedThreadStart(CopyFromLocalToRemote));
					copyForm = new CopyProgressForm(copyThread);
					copyThread.Start(filename);
					copyForm.ShowDialog();
				}
			}
			else // копируем с удаленного на локальный
			{
				int FilesAmount = RHFilesList.SelectedItems.Count;
				if (FilesAmount == 0)
					return;

				for (int i = 0; i < FilesAmount; i++)
				{
					string filename = RHFilesList.SelectedItems[i].Text + RHFilesList.SelectedItems[i].SubItems[1].Text;

					Thread copyThread = new Thread(new ParameterizedThreadStart(CopyFromRemoteToLocal));
					copyForm = new CopyProgressForm(copyThread);
					copyThread.Start(filename);
					copyForm.ShowDialog();
				}
			}
		}

		private void CopyButton_Click(object sender, EventArgs e)
		{
			Copy();
		}

		private void Move()
		{
			if (LHFilesList.Focused) // перемещаем с локального на удаленный
			{
				int FilesAmount = LHFilesList.SelectedItems.Count;
				if (FilesAmount == 0)
					return;

				for (int i = 0; i < FilesAmount; i++)
				{
					string filename = LHFilesList.SelectedItems[i].Text + LHFilesList.SelectedItems[i].SubItems[1].Text;

					Thread moveThread = new Thread(new ParameterizedThreadStart(MoveFromLocalToRemote));
					moveForm = new MoveProgressForm(moveThread);
					moveThread.Start(filename);
					moveForm.ShowDialog();
				}
			}
			else // перемещаем с удаленного на локальный
			{
				int FilesAmount = RHFilesList.SelectedItems.Count;
				if (FilesAmount == 0)
					return;

				for (int i = 0; i < FilesAmount; i++)
				{
					string filename = RHFilesList.SelectedItems[i].Text + RHFilesList.SelectedItems[i].SubItems[1].Text;

					Thread moveThread = new Thread(new ParameterizedThreadStart(MoveFromRemoteToLocal));
					moveForm = new MoveProgressForm(moveThread);
					moveThread.Start(filename);
					moveForm.ShowDialog();
				}
			}
		}

		private void MoveButton_Click(object sender, EventArgs e)
		{
			Move();
		}

		private void NewFolder()
		{
			if (LHFilesList.Focused)
			{
				NewFolderForm nfForm = new NewFolderForm();
				if (nfForm.ShowDialog() != DialogResult.OK)
					return;

				try
				{
					Directory.CreateDirectory(currentLocalPath + Path.DirectorySeparatorChar + nfForm.NFolderName);
				}
				catch (DirectoryNotFoundException)
				{
					MessageBox.Show("You should input valid folder name.", "Folder name is invalid!", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (PathTooLongException)
				{
					MessageBox.Show("Folder name is too long. Input shorter one", "Folder name error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (ArgumentNullException)
				{
					Directory.CreateDirectory(currentLocalPath + Path.DirectorySeparatorChar + "New folder");
				}
				catch
				{
					MessageBox.Show("Can't create folder!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}

				RefreshLists();
				return;
			}

			if (RHFilesList.Focused)
			{
				NewFolderForm nfForm = new NewFolderForm();
				if (nfForm.ShowDialog() != DialogResult.OK)
					return;

				try
				{
					slave.CreateDir(currentRemotePath + Path.DirectorySeparatorChar + nfForm.NFolderName);
				}
				catch (ArgumentNullException)
				{
					slave.CreateDir(currentRemotePath + Path.DirectorySeparatorChar + "New folder");
				}
				catch (PathTooLongException)
				{
					MessageBox.Show("Folder name is too long. Input shorter one", "Folder name error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch
				{
					MessageBox.Show("Can't create folder!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}

				RefreshLists();
				return;
			}
		}

		private void NewFolderButton_Click(object sender, EventArgs e)
		{
			NewFolder();
		}

		private void Delete()
		{
			if (LHFilesList.Focused)
			{
				int nOfFiles = LHFilesList.SelectedItems.Count;
				if (nOfFiles == 0)
					return;

				if (nOfFiles == 1)
				{
					if (MessageBox.Show("Are you sure that you want to delete this file?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
						return;
				}
				else
					if (MessageBox.Show("Are you sure that you want to delete these files?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
						return;

				string filename;

				// TODO: Deleting form with progressbar
				for (int i = 0; i < nOfFiles; i++)
				{
					filename = LHFilesList.SelectedItems[i].Text;
					File.Delete(currentLocalPath + Path.DirectorySeparatorChar + filename);
				}
				RefreshLists();
				return;
			}

			if (RHFilesList.Focused)
			{
				int nOfFiles = RHFilesList.SelectedItems.Count;
				if (nOfFiles == 0)
					return;

				if (nOfFiles == 1)
				{
					if (MessageBox.Show("Are you sure that you want to delete this file?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
						return;
				}
				else
					if (MessageBox.Show("Are you sure that you want to delete these files?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
						return;

				string filename;

				// TODO: Deleting form with progressbar
				for (int i = 0; i < nOfFiles; i++)
				{
					filename = RHFilesList.SelectedItems[i].Text;
					try
					{
						slave.DeleteFile(currentRemotePath + Path.DirectorySeparatorChar + filename);
					}
					catch (ArgumentNullException)
					{
						MessageBox.Show("Path is null", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
					catch (ArgumentException)
					{
						MessageBox.Show("Path is invalid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
					catch (UnauthorizedAccessException)
					{
						MessageBox.Show("You have no rights to delete this file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
					catch (PathTooLongException)
					{
						MessageBox.Show("Path you input is too long", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
					catch (NotSupportedException)
					{
						MessageBox.Show("Invalid file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
					catch (DirectoryNotFoundException)
					{
						MessageBox.Show("Path is invalid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}			
				}

				RefreshLists();
				return;
			}
		}

		private void Rename(string oldname, string newname)
		{
			if (LHFilesList.Focused)
			{
				try
				{
					File.Move(currentLocalPath + oldname, currentLocalPath + newname);
				}
				#region Error Handling
				catch (ArgumentNullException)
				{
					MessageBox.Show("Please input filename", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (ArgumentException)
				{
					MessageBox.Show("Input valid filename", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (UnauthorizedAccessException)
				{
					MessageBox.Show("You have no rights to rename this file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (FileNotFoundException)
				{
					MessageBox.Show("File not found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (PathTooLongException)
				{
					MessageBox.Show("Path you input is too long", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (NotSupportedException)
				{
					MessageBox.Show("Invalid file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				#endregion
				
				RefreshLists();
			}

			if (RHFilesList.Focused)
			{
				try
				{
					slave.MoveFile(currentRemotePath + oldname, currentRemotePath + newname);
				}
				catch (IOException)
				{
					MessageBox.Show("Input/output error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (ArgumentNullException)
				{
					MessageBox.Show("Please input filename", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (ArgumentException)
				{
					MessageBox.Show("Input valid filename", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (UnauthorizedAccessException)
				{
					MessageBox.Show("You have no rights to rename this file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				catch (NotSupportedException)
				{
					MessageBox.Show("Invalid file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				RefreshLists();
			}
		}

		private void DeleteButton_Click(object sender, EventArgs e)
		{
			Delete();
		}

		private void copyToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Copy();
		}

		private void moveToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Move();
		}

		private void newFoldeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			NewFolder();
		}

		private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Delete();
		}

		private void RenameStart()
		{
			if (LHFilesList.Focused)
			{
				LHFilesList.BeginEdit();
			}
			else
			{
				RHFilesList.BeginEdit();
			}
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			RenameStart();
		}				
	}
}
